<template>
  <el-container class="home">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../../assets/img/head.jpg" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logOut" class="logout">退出登录</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse? '64px' : '200px'">
        <!-- 菜单折叠 -->
        <div class="toggle-button" @click="toggleCollapse">
          |||
        </div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePaht"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 一级图标 -->
              <i :class="icons[item.id]"></i>
              <!-- 一级文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="seveNavClick('/' + subItem.path)">
              <!-- 二级菜单模板 -->
              <template slot="title">
                <!-- 二级图标 -->
                <i class="el-icon-menu"></i>
                <!-- 二级文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体 -->
      <el-main>
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      menulist: [],
      icons: {
        '125': 'el-icon-s-custom',
        '103': 'el-icon-s-unfold',
        '101': 'el-icon-s-goods',
        '102': 'el-icon-s-order',
        '145': 'el-icon-s-marketing'
      },
      //菜单栏是否折叠 
      isCollapse: false,
      //激活的链接地址
      activePaht: ''
    };
  },
  created() {
    this.getMenuList()
    this.activePaht = window.sessionStorage.getItem('activePath')
    console.log(this.activePaht)
  },
  methods: {
    //退出登录
    logOut() {
      //清除token
      window.sessionStorage.clear();
      //返回登录页面
      this.$router.push("/login");
    },
    //获取菜单栏数据
    async getMenuList() {
      const {data: res} = await this.$http.get('menus')
      if(res.meta.status !== 200) return this.$message.error('res.meat.msg')
      this.menulist = res.data
    },
    //菜单栏折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    //保存链接激活状态
    seveNavClick(activePaht) {
      window.sessionStorage.setItem('activePath', activePaht)
      this.activePaht = activePaht
    }
  }
};
</script>

<style lang="less" scoped>
.home {
  height: 100%;
}
.el-header {
  background-color: #333744;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }
    span {
      margin-left: 15px;
      color: #ffffff;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  color: #ffffff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
  height: 30px;
  line-height: 30px;
  font-size: 14px;
}
</style>